package com.itheima.service.impl;

import com.itheima.dao.mapper.BookMapper;
import com.itheima.pojo.Book;
import com.itheima.pojo.Borrow;
import com.itheima.pojo.History;
import com.itheima.pojo.PageBean;
import com.itheima.service.BookService;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BookServiceImpl implements BookService {
    //1.创建工厂对象
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public List<Book> selectAll() {
        SqlSession sqlSession = factory.openSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        List<Book> book = mapper.selectAll();
        sqlSession.close();
        return book;
    }

    @Override
    public boolean add(Book book) {
        SqlSession sqlSession = factory.openSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        boolean flag = mapper.add(book);
        sqlSession.commit();
        sqlSession.close();
        return flag;
    }

    @Override
    public void deleteByIds(int[] ids) {
        SqlSession sqlSession = factory.openSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        mapper.deleteByIds(ids);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void delete(int id) {
        SqlSession sqlSession = factory.openSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        mapper.delete(id);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public int update(Book book) {
        SqlSession sqlSession = factory.openSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        int i = mapper.update(book);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }


    @Override
    public PageBean<Book> selectByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = factory.openSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);

        //计算开始索引
        int begin = (currentPage - 1)*pageSize;
        //计算查询条目数
        int size = pageSize;

        //查询当前页数据
        List<Book> rows = mapper.selectByPage(begin, size);

        //查询总记录数
        int totalCount = mapper.selectTotalCount();

        //封装PageBean对象
        PageBean<Book> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);

        sqlSession.close();

        return pageBean;

    }

    @Override
    public PageBean<Book> selectByPageAndCondition(int currentPage, int pageSize, Book book) {
        SqlSession sqlSession = factory.openSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);

        //计算开始索引
        int begin = (currentPage - 1)*pageSize;
        //计算查询条目数
        int size = pageSize;

            String name = book.getName();
            if (name!=null&&name.length()>0){
                book.setName("%"+name+"%");
            }

        //查询当前页数据
        List<Book> rows = mapper.selectByPageAndCondition(begin, size, book);

        //查询总记录数
        int totalCount = mapper.selectTotalCountByCondition(book);

        //封装PageBean对象
        PageBean<Book> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);

        sqlSession.close();

        return pageBean;
    }

    @Override
    public Book selectById(int id) {
        SqlSession sqlSession = factory.openSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        Book book = mapper.selectById(id);
        sqlSession.close();
        return book;
    }

    @Override
    public void borrow(Borrow borrow) {
        SqlSession sqlSession = factory.openSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        mapper.borrow(borrow);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public List<Borrow> selectByUsername(String username) {
        SqlSession sqlSession = factory.openSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        List<Borrow> list = mapper.selectByUsername(username);
        sqlSession.close();
        return list;
    }

    @Override
    public void deleteBorrow(int id) {
        SqlSession sqlSession = factory.openSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        mapper.deleteBorrow(id);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public Borrow getBook(int id) {
        SqlSession sqlSession = factory.openSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        Borrow borrowBook = mapper.getBook(id);
        sqlSession.close();
        return borrowBook;
    }

    @Override
    public Book selectByName(String name) {
        SqlSession sqlSession = factory.openSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        Book book = mapper.selectByName(name);
        sqlSession.close();
        return book;
    }

    @Override
    public void huanShu(Book book) {
        SqlSession sqlSession = factory.openSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        mapper.huanShu(book);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public List<Borrow> selectFromBorrow() {
        SqlSession sqlSession = factory.openSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        List<Borrow> list = mapper.selectFromBorrow();
        sqlSession.close();
        return list;
    }

    @Override
    public int deleteUser(String username) {
        SqlSession sqlSession = factory.openSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        int i = mapper.deleteUser(username);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    @Override
    public boolean addHistory(History history) {
        SqlSession sqlSession = factory.openSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        boolean flag = mapper.addHistory(history);
        sqlSession.commit();
        sqlSession.close();
        return flag;
    }

    @Override
    public int deleteHistory(String username) {
        SqlSession sqlSession = factory.openSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        int i = mapper.deleteHistory(username);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }
}
